using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EmergencyicPanel : BasePanel, IInitializable
{
    private int price = 100;
    // Start is called before the first frame update
    public void Initialize(object data)
    {
        Debug.Log("EmergencyicPanel initialized" + data);
    }
    public void OnCloseClick()
    {
        // 关闭面板
        PanelManager.Instance.ClosePanel<EmergencyicPanel>();
        //合成新的Goods
        GlobalEventManager.EmitEvent("SynthesizeNewFood");
    }

    public override void OnOpen()
    {
        base.OnOpen(); // 调用基类的 OnOpen 方法

        StartCoroutine(DelayShowNoThanks());
    }

    public override void OnClose()
    {
        base.OnClose(); // 调用基类的 OnClose 方法
    }

    public void OnBuyClick()
    {
        // 购买
        Debug.Log("购买");
        float coins = (float)UserManager.Instance.GetUserInfo()["Coins"];
        if (coins < price)
        {
            Tools.ShowTipToast("Insufficient coins");
            return;
        }
        UserManager.Instance.SubtractCoins(price);

        // 购买成功
        UserManager.Instance.SetIsEmergencyic(true);

        //消除
        GlobalEventManager.EmitEvent("EmergencyicBuySuccess");

        PanelManager.Instance.ClosePanel<EmergencyicPanel>();

    }

    private IEnumerator DelayShowNoThanks()
    {
        Transform tf = transform.Find("Bg/NoThanks");
        if (tf != null)
        {
            tf.gameObject.SetActive(false);
            yield return new WaitForSeconds(1f);
            tf.gameObject.SetActive(true);
        }
        else
        {
            Debug.LogError("NoThanks not found");
        }
    }

}
